{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "smoking-comparative",
   "metadata": {},
   "source": [
    "# Interactivity\n",
    "\n",
    "The visualizations consume the Interpret API, and is responsible\n",
    "for both displaying explanations and the underlying rendering infrastructure."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "light-steps",
   "metadata": {},
   "source": [
    "## Visualizing with the show method\n",
    "\n",
    "Interpret exposes a top-level method `show`, of which acts as the surface for rendering explanation visualizations. This can produce either a drop down widget or dashboard depending on what's provided.\n",
    "\n",
    "### Show a single explanation\n",
    "\n",
    "For basic use cases, it is good to show an explanation one at a time. The rendered widget will provide a dropdown to select between visualizations. For example, in the event of a global explanation, it will provide an overview, along with graphs for each feature as shown with the code below:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "gross-river",
   "metadata": {},
   "outputs": [],
   "source": [
    "from interpret import set_visualize_provider\n",
    "from interpret.provider import InlineProvider\n",
    "set_visualize_provider(InlineProvider())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "threaded-conservative",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import pandas as pd\n",
    "from sklearn.model_selection import train_test_split\n",
    "\n",
    "from interpret.glassbox import ExplainableBoostingClassifier\n",
    "from interpret import show\n",
    "\n",
    "df = pd.read_csv(\n",
    "    \"https://archive.ics.uci.edu/ml/machine-learning-databases/adult/adult.data\",\n",
    "    header=None)\n",
    "df.columns = [\n",
    "    \"Age\", \"WorkClass\", \"fnlwgt\", \"Education\", \"EducationNum\",\n",
    "    \"MaritalStatus\", \"Occupation\", \"Relationship\", \"Race\", \"Gender\",\n",
    "    \"CapitalGain\", \"CapitalLoss\", \"HoursPerWeek\", \"NativeCountry\", \"Income\"\n",
    "]\n",
    "seed = 42\n",
    "np.random.seed(seed)\n",
    "df = df.sample(frac=0.05, random_state=seed)\n",
    "train_cols = df.columns[0:-1]\n",
    "label = df.columns[-1]\n",
    "X = df[train_cols]\n",
    "y = df[label]\n",
    "\n",
    "X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.20, random_state=seed)\n",
    "\n",
    "ebm = ExplainableBoostingClassifier()\n",
    "ebm.fit(X_train, y_train)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "southeast-cemetery",
   "metadata": {},
   "outputs": [],
   "source": [
    "ebm_global = ebm.explain_global()\n",
    "show(ebm_global)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "vkrkskhflkjs",
   "metadata": {},
   "source": [
    "<br/>\n",
    "<br/>\n",
    "<br/>\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ahead-tobago",
   "metadata": {},
   "source": [
    "### Show a specific visualization within an explanation\n",
    "\n",
    "Let's say you are after one specific visualization within an explanation, then you can specify it with a key as the subsequent function argument."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "social-module",
   "metadata": {},
   "outputs": [],
   "source": [
    "show(ebm_global, \"Age\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "qkfdlklkjss",
   "metadata": {},
   "source": [
    "<br/>\n",
    "<br/>\n",
    "<br/>\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "entitled-adjustment",
   "metadata": {},
   "source": [
    "### Show multiple explanations for comparison\n",
    "\n",
    "If you running in a local environment (such as a running Python on your laptop), then `show` can expose a dashboard for comparison which can be invoked the in the following way (provide a list of explanations in the first argument):"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "greater-bulgaria",
   "metadata": {},
   "outputs": [],
   "source": [
    "from interpret.glassbox import LogisticRegression, ClassificationTree\n",
    "\n",
    "# We have to transform categorical variables to use Logistic Regression and Decision Tree\n",
    "X_enc = pd.get_dummies(X, prefix_sep='.')\n",
    "feature_names = list(X_enc.columns)\n",
    "X_train_enc, X_test_enc, y_train, y_test = train_test_split(X_enc, y, test_size=0.20, random_state=seed)\n",
    "\n",
    "lr = LogisticRegression(random_state=seed, feature_names=feature_names, penalty='l1', solver='liblinear')\n",
    "lr.fit(X_train_enc, y_train)\n",
    "\n",
    "lr_global = lr.explain_global()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "unauthorized-stereo",
   "metadata": {},
   "outputs": [],
   "source": [
    "show([ebm_global, lr_global])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "bound-investing",
   "metadata": {},
   "source": [
    "# Interpret API\n",
    "\n",
    "The API is responsible for standardizing ML interpretability\n",
    "explainers and explanations, providing a consistent interface for both users\n",
    "and developers. To support this, it also provides foundational top-level methods that support\n",
    "visualization and data access.\n",
    "\n",
    "**Explainers** are glassbox or blackbox algorithms that will produce an **explanation**, an artifact that is ready for visualizations or further data processing.\n",
    "\n",
    "## Explainer\n",
    "\n",
    "An explainer will produce an explanation from its `.explain_*` method. These explanations normally provide an understanding of **global** model behavior or **local** individual predictions (`.explain_global` and `.explain_local` respectively).\n",
    "\n",
    "```{eval-rst}\n",
    ".. autoclass:: interpret.api.base.ExplainerMixin\n",
    "   :members:\n",
    "   :inherited-members:\n",
    "```\n",
    "\n",
    "## Explanation\n",
    "\n",
    "An explanation is a self-contained object that help understands either its target model behavior, or a set of individual predictions. The explanation should provide access to visualizations through `.visualize`, and data processing the `.data` method. Both `.visualize` and `.data` should share the same function signature in terms of arguments.\n",
    "\n",
    "```{eval-rst}\n",
    ".. autoclass:: interpret.api.base.ExplanationMixin\n",
    "   :members:\n",
    "   :inherited-members:\n",
    "```\n",
    "\n",
    "## Show\n",
    "\n",
    "The `show` method is used as a universal function that provides visualizations for whatever explanation(s) is provided in its arguments. Implementation-wise it will provide some visualization platform (i.e. a dashboard or widget) and expose the explanation(s)' visualizations as given by the `.visualize` call.\n",
    "\n",
    "```{eval-rst}\n",
    ".. autoclass:: interpret.show\n",
    "   :members:\n",
    "   :inherited-members:\n",
    "```"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "cubic-gregory",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "language_info": {
   "name": "python"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
